
package com.cari.baju.yog;

import android.app.Application;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.cari.baju.yog.utils.Utils;

public class BaseApplication extends Application {

    private RequestQueue requestQueue;
    private static BaseApplication instance;
    private static final int TIMEOUT_MS = 25000;

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;
    }

    public static synchronized BaseApplication getInstance() {
        return instance;
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null)
            requestQueue = Volley.newRequestQueue(getApplicationContext());
        return requestQueue;
    }

    public <T> void addToRequestQueue(Request<T> request, String TAG) {
        Utils.TRACE("BASE", request.getUrl());
        request.setTag(TAG);

        request.setRetryPolicy(new DefaultRetryPolicy(
                TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        getRequestQueue().add(request);
    }

    public void cancelPendingRequest(String TAG) {
        if (requestQueue != null)
            requestQueue.cancelAll(TAG);
    }
}
